/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#ifndef SQUADSELECTION_HPP
#define SQUADSELECTION_HPP

#include "Spatial.hpp"

#include "ForwardDecls.hpp"

namespace spelprog2 {
namespace presentation {

/** Copyable class that represents a selection of a squad (and its owner).
    */
class SquadSelection {
    const sim::Int player_;
    const sim::Int squad_;
public:
    SquadSelection(sim::Int player, sim::Int squad) : player_(player), squad_(squad) { }
    sim::Int getPlayer() const { return player_; }
    sim::Int getSquad() const { return squad_; }

    bool operator< (const SquadSelection & rhs) const {
        if (player_ == rhs.player_) {
            return squad_ < rhs.squad_;
        }

        return player_ < rhs.player_;
    }
};

/** Copyable class that manages a set of squad selections. Will automatically filter out invalid requests.
    Selections must be either completely friendly or completely enemy.
    */
class SquadSelections
{
    typedef std::set<SquadSelection> SelectionSet;

    sim::WorldManager * worldMan_; //not a reference as this must be copyable
    SelectionSet selections_;
    bool allowEnemy_;
public:
    typedef SelectionSet::const_iterator ConstIterator;
    typedef std::pair<ConstIterator, ConstIterator> ConstIteratorRange;

    //! Constructs this object. Requires a link to the world manager and a flag for whether enemy squads can be selected.
    SquadSelections(sim::WorldManager & worldMan, bool allowEnemy);

    //! Removes all selected squads.
    void clear();

    //! Returns true if one or more friendly units are selected.
    bool friendlySelected() const;

    //! Returns true if one or more enemy units are selected.
    bool enemySelected() const;

    //! Attempts to a squad to the selection. Returns true if successful.
    bool add(const SquadSelection & sel);

    //! Attempts to remove a squad from the selection. Returns true if successful.
    bool remove(const SquadSelection & sel);

    //! Returns true if a selection is present in this selection set.
    bool contains(const SquadSelection & sel) const { return selections_.find(sel) != selections_.end(); }

    //! Gets a pair representating the iterable const range of all squads in this selection.
    ConstIteratorRange getAll() const { return ConstIteratorRange(selections_.begin(), selections_.end()); }
};

}; //presentation
}; //spelprog2

#endif //SQUADSELECTION_HPP
